package database

import (
	"log"
	"rbac-system/models"
)

// AutoMigrate 自动迁移数据库表结构
func AutoMigrate() {
	err := DB.AutoMigrate(
		&models.User{},
		&models.Role{},
		&models.Permission{},
		&models.RolePermission{},
		&models.UserRole{},
	)

	if err != nil {
		log.Fatalf("数据库迁移失败: %v", err)
	}

	log.Println("数据库表结构迁移完成")
}

// InitData 初始化基础数据
func InitData() {
	// 创建基础角色
	roles := []models.Role{
		{Name: "超级管理员", Description: "系统超级管理员，拥有所有权限"},
		{Name: "管理员", Description: "系统管理员，拥有大部分管理权限"},
		{Name: "普通用户", Description: "普通用户，拥有基本操作权限"},
	}

	for _, role := range roles {
		var existingRole models.Role
		if err := DB.Where("name = ?", role.Name).First(&existingRole).Error; err != nil {
			DB.Create(&role)
		}
	}

	// 创建基础权限
	permissions := []models.Permission{
		// 系统管理菜单
		{Name: "系统管理", Code: "system", Type: 1, ParentID: 0, Path: "/system"},
		{Name: "用户管理", Code: "user:manage", Type: 1, ParentID: 1, Path: "/system/user"},
		{Name: "角色管理", Code: "role:manage", Type: 1, ParentID: 1, Path: "/system/role"},
		{Name: "权限管理", Code: "permission:manage", Type: 1, ParentID: 1, Path: "/system/permission"},

		// 用户管理按钮权限
		{Name: "用户查询", Code: "user:query", Type: 2, ParentID: 2},
		{Name: "用户新增", Code: "user:create", Type: 2, ParentID: 2},
		{Name: "用户编辑", Code: "user:update", Type: 2, ParentID: 2},
		{Name: "用户删除", Code: "user:delete", Type: 2, ParentID: 2},

		// 角色管理按钮权限
		{Name: "角色查询", Code: "role:query", Type: 2, ParentID: 3},
		{Name: "角色新增", Code: "role:create", Type: 2, ParentID: 3},
		{Name: "角色编辑", Code: "role:update", Type: 2, ParentID: 3},
		{Name: "角色删除", Code: "role:delete", Type: 2, ParentID: 3},

		// 权限管理按钮权限
		{Name: "权限查询", Code: "permission:query", Type: 2, ParentID: 4},
		{Name: "权限新增", Code: "permission:create", Type: 2, ParentID: 4},
		{Name: "权限编辑", Code: "permission:update", Type: 2, ParentID: 4},
		{Name: "权限删除", Code: "permission:delete", Type: 2, ParentID: 4},

		// 接口权限
		{Name: "用户API", Code: "user:api", Type: 3, ParentID: 0},
		{Name: "角色API", Code: "role:api", Type: 3, ParentID: 0},
		{Name: "权限API", Code: "permission:api", Type: 3, ParentID: 0},
	}

	for _, permission := range permissions {
		var existingPermission models.Permission
		if err := DB.Where("code = ?", permission.Code).First(&existingPermission).Error; err != nil {
			DB.Create(&permission)
		}
	}

	// 为超级管理员角色分配所有权限
	var superAdminRole models.Role
	var allPermissions []models.Permission

	if err := DB.Where("name = ?", "超级管理员").First(&superAdminRole).Error; err == nil {
		DB.Find(&allPermissions)
		for _, permission := range allPermissions {
			var existingRolePermission models.RolePermission
			if err := DB.Where("role_id = ? AND permission_id = ?", superAdminRole.ID, permission.ID).First(&existingRolePermission).Error; err != nil {
				rolePermission := models.RolePermission{
					RoleID:       superAdminRole.ID,
					PermissionID: permission.ID,
				}
				DB.Create(&rolePermission)
			}
		}
	}

	// 创建默认管理员用户
	hashedPassword := "$2a$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi" // password
	adminUser := models.User{
		Username: "admin",
		Password: hashedPassword,
		Email:    "admin@example.com",
		Phone:    "13800138000",
		Status:   1,
	}

	var existingUser models.User
	if err := DB.Where("username = ?", "admin").First(&existingUser).Error; err != nil {
		DB.Create(&adminUser)

		// 为管理员用户分配超级管理员角色
		if err := DB.Where("name = ?", "超级管理员").First(&superAdminRole).Error; err == nil {
			userRole := models.UserRole{
				UserID: adminUser.ID,
				RoleID: superAdminRole.ID,
			}
			DB.Create(&userRole)
		}
	}

	log.Println("基础数据初始化完成")
}
